package com.gs.SynPatrolRep.presenter;

import com.google.gson.reflect.TypeToken;
import com.gs.SynPatrolRep.apiresult.ApiResultData;
import com.gs.SynPatrolRep.apiresult.PagedList;
import com.gs.SynPatrolRep.db.dto.CreatePalBillItemDto;
import com.gs.SynPatrolRep.db.dto.PalBillItemDto;
import com.gs.SynPatrolRep.db.query.BaseQuery;
import com.gs.SynPatrolRep.model.PalBillModel;
import com.gs.SynPatrolRep.model.UtilModel;
import com.gs.SynPatrolRep.model.listener.OnResultListener;
import com.gs.SynPatrolRep.view.IPalBillItemView;

import java.io.IOException;

public class PalBillItemPresenter extends BasePresenter<IPalBillItemView> {
    private PalBillModel model = new PalBillModel();
    private UtilModel utilsModel = new UtilModel();

    public void getData(BaseQuery query) {
        getView().showLoading();
        model.getPalBillsItems(query, new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().onGetDataSuccess((ApiResultData<PagedList<PalBillItemDto>>) aipResult);
                    getView().hideLoading();
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onGetDataFails(e);
                    getView().hideLoading();
                }
            }
        }, new TypeToken<ApiResultData<PagedList<PalBillItemDto>>>() {
        }.getType());
    }

    public void uploadData() {
        getView().showLoading();
        CreatePalBillItemDto bean = new CreatePalBillItemDto();
        bean.setBillId(getView().getBillId());
        bean.setSiteId(getView().getSiteId());
        bean.setExct(getView().getExct());
        bean.setExctRemark(getView().getExctRemark());
        bean.setItemFdbacks(getView().getItemFdbacks());
        bean.setExctPhotos(getView().getExctPhotos());
        if (getView().getEvent().getLevelType() != 0) {
            bean.setEvent(getView().getEvent());
        }
        model.uploadData(bean, new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onUploadDataSuccess((ApiResultData<String>) aipResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onUploadDataFails(e);
                }
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }

    public void uploadImg(String img, int position) throws IOException {
        getView().showLoading();
        utilsModel.upload(img, new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().onUploadImgSuccess((ApiResultData<String>) aipResult,position);
                    getView().hideLoading();
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onUploadImgFails(e,position);
                    getView().hideLoading();
                }
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }
}
